# Copyright (C) 2009 Paul Rudin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from enthought.mayavi import mlab
from enthought.tvtk.api import tvtk

def view_vox(voxels):
    src = mlab.pipeline.scalar_field(voxels * 128)
    mlab.pipeline.volume(src)
    mlab.show()

def view_tri(model):
    p = model.points
    model = mlab.triangular_mesh(p[:,0],p[:,1],p[:,2],
                      model.polys, color=(0.8,0.5,0.5),
                      representation = 'surface')
    mlab.show()
    
class ModelViewer(object):

    def __init__(self, model):
        self.model = model

    def tvtk_init(self):
        self.renderer = tvtk.Renderer()
        render_window = tvtk.RenderWindow(size=(500,500))
        render_window.add_renderer(self.renderer)
        self.interactor = tvtk.RenderWindowInteractor(render_window=render_window)

    def make_scene(self):
        mesh = tvtk.PolyData(polys = self.model.polys,
                             points = self.model.points)
        mapper  = tvtk.PolyDataMapper(input = mesh)
        actor = tvtk.Actor(mapper = mapper)
        self.renderer.add_actor(actor)

    def view(self):
        self.tvtk_init()
        self.make_scene()
        self.interactor.initialize()
        self.interactor.start()

def view_poly(model):
    ModelViewer(model).view()
